home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / Blobs / Blobs.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  7.6 KB  |  283 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Blobs.fx
  3. //
  4. // Desc: Effect file for the Blobs sample. 
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8.  
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Global variables
  12. //-----------------------------------------------------------------------------
  13. static const float GAUSSIANTEXSIZE = 64;
  14. static const float TEMPTEXSIZE = 1024;
  15. static const float THRESHOLD = 0.08f;
  16.  
  17. float4x4 g_mWorldViewProjection;
  18.  
  19. // Textures
  20. texture g_tSourceBlob;
  21. texture g_tNormalBuffer;
  22. texture g_tColorBuffer;
  23. texture g_tEnvMap;
  24.  
  25.  
  26. //-----------------------------------------------------------------------------
  27. // Samplers
  28. //-----------------------------------------------------------------------------
  29. sampler SourceBlobSampler = 
  30. sampler_state
  31. {
  32.     Texture = <g_tSourceBlob>;
  33.     MinFilter = Point;
  34.     MagFilter = Point;
  35.     MipFilter = Point;
  36.  
  37.     AddressU = Clamp;
  38.     AddressV = Clamp;
  39. };
  40.  
  41. sampler NormalBufferSampler = 
  42. sampler_state
  43. {
  44.     Texture = <g_tNormalBuffer>;
  45.     MinFilter = Point;
  46.     MagFilter = Point;
  47.     MipFilter = Point;
  48.  
  49.     AddressU = Clamp;
  50.     AddressV = Clamp;
  51. };
  52.  
  53. sampler ColorBufferSampler = 
  54. sampler_state
  55. {
  56.     Texture = <g_tColorBuffer>;
  57.     MinFilter = Point;
  58.     MagFilter = Point;
  59.     MipFilter = Point;
  60.  
  61.     AddressU = Clamp;
  62.     AddressV = Clamp;
  63. };
  64.  
  65. sampler EnvMapSampler =
  66. sampler_state
  67. {
  68.     Texture = <g_tEnvMap>;
  69.     MinFilter = Linear;
  70.     MagFilter = Linear;
  71.     MipFilter = Linear;
  72.  
  73.     AddressU = Clamp;
  74.     AddressV = Clamp;
  75. };
  76.  
  77. //-----------------------------------------------------------------------------
  78. // Vertex/pixel shader output structures
  79. //-----------------------------------------------------------------------------
  80. struct VS_OUTPUT
  81. {
  82.     float4 vPosition : POSITION;
  83.     float2 tCurr     : TEXCOORD0;
  84.     float2 tBack     : TEXCOORD1;
  85.     float  fSize     : TEXCOORD2;
  86.     float3 vColor    : TEXCOORD3;
  87. };
  88.  
  89.  
  90. struct PS_OUTPUT
  91. {
  92.     float4 vColor[2] : COLOR;
  93. };
  94.  
  95.  
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: DoLerp
  100. // Type: Helper function                                      
  101. // Desc: Peform a linear interpolation
  102. //-----------------------------------------------------------------------------
  103. void DoLerp( in float2 tCurr, out float4 outval )
  104. {
  105.     // Scale out into pixel space
  106.     float2 pixelpos = GAUSSIANTEXSIZE * tCurr;
  107.  
  108.     // Determine the lerp amounts
  109.     float2 lerps = frac( pixelpos );
  110.  
  111.     // Get the upper left position
  112.     float3 lerppos = float3((pixelpos-(lerps/GAUSSIANTEXSIZE))/GAUSSIANTEXSIZE,0);
  113.  
  114.     float4 sourcevals[4];
  115.     sourcevals[0] = tex2D( SourceBlobSampler, lerppos );  
  116.     sourcevals[1] = tex2D( SourceBlobSampler, lerppos + float3(1.0/GAUSSIANTEXSIZE, 0,0) );
  117.     sourcevals[2] = tex2D( SourceBlobSampler, lerppos + float3(0, 1.0/GAUSSIANTEXSIZE,0) );
  118.     sourcevals[3] = tex2D( SourceBlobSampler, lerppos + float3(1.0/GAUSSIANTEXSIZE, 1.0/GAUSSIANTEXSIZE,0) );
  119.  
  120.     // Bilinear filtering
  121.     outval = lerp( lerp( sourcevals[0], sourcevals[1], lerps.x ),
  122.                    lerp( sourcevals[2], sourcevals[3], lerps.x ),
  123.                    lerps.y );
  124. }
  125.  
  126.  
  127. //-----------------------------------------------------------------------------
  128. // Name: BlobBlenderPS
  129. // Type: Pixel shader
  130. // Desc: 
  131. //-----------------------------------------------------------------------------
  132. PS_OUTPUT BlobBlenderPS( VS_OUTPUT Input )
  133.     PS_OUTPUT output;
  134.     float4 weight;
  135.    
  136.     // Get the new blob weight
  137.     DoLerp( Input.tCurr, weight );
  138.  
  139.     // Get the old data
  140.     float4 oldNormalData = tex2D( NormalBufferSampler, Input.tBack );
  141.     float4 oldColorData  = tex2D( ColorBufferSampler, Input.tBack );
  142.     
  143.     // Generate new surface data
  144.     float4 newNormalData = float4((Input.tCurr.x-0.5) * Input.fSize,
  145.                                 (Input.tCurr.y-0.5) * Input.fSize,
  146.                                 0,
  147.                                 1);
  148.     newNormalData *= weight.r;
  149.     
  150.     //generate new material properties
  151.     float4 newColorData = float4(Input.vColor.r,
  152.                                Input.vColor.g,
  153.                                Input.vColor.b,
  154.                                0);
  155.     newColorData *= weight.r;
  156.     
  157.     // Additive blending
  158.     output.vColor[0] = newNormalData + oldNormalData; 
  159.     output.vColor[1] = newColorData + oldColorData;
  160.     
  161.     return output;
  162. }
  163.  
  164.  
  165. float4 BlobBlenderPSNormal( VS_OUTPUT Input ) : COLOR0
  166.     float4 weight;
  167.  
  168.     // Get the new blob weight
  169.     DoLerp( Input.tCurr, weight );
  170.  
  171.     // Get the old data
  172.     float4 oldNormalData = tex2D( NormalBufferSampler, Input.tBack );
  173.  
  174.     // Generate new surface data
  175.     float4 newNormalData = float4((Input.tCurr.x-0.5) * Input.fSize,
  176.                                 (Input.tCurr.y-0.5) * Input.fSize,
  177.                                 0,
  178.                                 1);
  179.     newNormalData *= weight.r;
  180.  
  181.     // Additive blending
  182.     return newNormalData + oldNormalData;
  183. }
  184.  
  185.  
  186. float4 BlobBlenderPSColor( VS_OUTPUT Input ) : COLOR0
  187. {
  188.     PS_OUTPUT output;
  189.     float4 weight;
  190.  
  191.     // Get the new blob weight
  192.     DoLerp( Input.tCurr, weight );
  193.  
  194.     // Get the old data
  195.     float4 oldColorData  = tex2D( ColorBufferSampler, Input.tBack );
  196.  
  197.     //generate new material properties
  198.     float4 newColorData = float4(Input.vColor.r,
  199.                                Input.vColor.g,
  200.                                Input.vColor.b,
  201.                                0);
  202.     newColorData *= weight.r;
  203.  
  204.     // Additive blending
  205.     return newColorData + oldColorData;
  206. }
  207.  
  208.  
  209. //-----------------------------------------------------------------------------
  210. // Name: BlobLightPS
  211. // Type: Pixel shader
  212. // Desc: 
  213. //-----------------------------------------------------------------------------
  214. float4 BlobLightPS( VS_OUTPUT Input ) : COLOR
  215. {
  216.     static const float aaval = THRESHOLD * 0.07f;
  217.  
  218.     float4 blobdata = tex2D( SourceBlobSampler, Input.tCurr);
  219.     float4 color = tex2D( ColorBufferSampler, Input.tCurr);
  220.     
  221.     color /= blobdata.w;
  222.     
  223.     float3 surfacept = float3(blobdata.x/blobdata.w,
  224.                               blobdata.y/blobdata.w, 
  225.                               blobdata.w-THRESHOLD); 
  226.     float3 thenorm = normalize(-surfacept);
  227.     thenorm.z = -thenorm.z;
  228.  
  229.     float4 Output;  
  230.     Output.rgb = color.rgb + texCUBE( EnvMapSampler, thenorm );
  231.     Output.rgb *= saturate ((blobdata.a - THRESHOLD)/aaval);
  232.     Output.a=1;
  233.     
  234.     return Output;
  235. }
  236.  
  237.  
  238.  
  239.  
  240. //-----------------------------------------------------------------------------
  241. // Name: BlobBlend
  242. // Type: Technique                                     
  243. // Desc: 
  244. //-----------------------------------------------------------------------------
  245. technique BlobBlend
  246. {
  247.     pass P0
  248.     {   
  249.         PixelShader = compile ps_2_0 BlobBlenderPS();
  250.     }
  251. }
  252.  
  253.  
  254. technique BlobBlendTwoPasses
  255. {
  256.     pass P0
  257.     {
  258.         PixelShader = compile ps_2_0 BlobBlenderPSNormal();
  259.     }
  260.     pass P1
  261.     {
  262.         PixelShader = compile ps_2_0 BlobBlenderPSColor();
  263.     }
  264. }
  265.  
  266.  
  267. //-----------------------------------------------------------------------------
  268. // Name: BlobLight
  269. // Type: Technique                                     
  270. // Desc: 
  271. //-----------------------------------------------------------------------------
  272. technique BlobLight
  273. {
  274.     pass P0
  275.     {   
  276.         PixelShader  = compile ps_2_0 BlobLightPS();
  277.     }
  278. }
  279.  
  280.  
  281.